package com.zxl.live.tools.tools;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/7/5.
 */
public class HttpUtils {

    public static byte[] doGet(String url) {
        try {
            LogUtils.d("doGet : " + url);
            Response response = new OkHttpClient().newCall(new Request.Builder().url(url).build()).execute();
            if (response.isSuccessful() && !response.isRedirect()) {
                return response.body().bytes();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void doGet(String url, Callback callback) {
        LogUtils.d("doGet : " + url);
        new OkHttpClient().newCall(new Request.Builder().url(url).build()).enqueue(callback);
    }

    public static byte[] doPost(String url, JSONObject jsonObject) {
        return doPost(url, jsonObject.toString());
    }

    public static byte[] doPost(String url, String params) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), params);
        LogUtils.d("doPost : " + url + ", " + requestBody.toString());
        try {
            Response response = new OkHttpClient().newCall(new Request.Builder().url(url).post(requestBody).build()).execute();
            if (response.isSuccessful() && !response.isRedirect()) {
                return response.body().bytes();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
